package com.example.miscontactos.Entidades;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;

public class Organizacion 
{
	private String organization;
 	private String title;
 	
 	public static Organizacion GetOrganizacionContacto(ContentResolver cr, String id)
 	{
 		//Cursor orgCur = cr.query(Data.CONTENT_URI,
			//	new String[] {Organization.TITLE,Organization.COMPANY}, Data.CONTACT_ID + " = " + id,null,null);
 		
 		String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
 	    String[] orgWhereParams = new String[]{id, 
 	        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
 	    Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, 
 	                null, orgWhere, orgWhereParams, null);
 	    
 	    Organizacion org = null;
 		if (orgCur.moveToFirst())
 		{
 			String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
 			String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY));
        	org = new Organizacion();
     		org.organization = orgName;
     		org.title = title;
 		}
 		
 		orgCur.close();
 		
 		return org;
 	}
 	
 	@Override
 	public String toString()
 	{
 		StringBuilder result = new StringBuilder();
    	result.append(this.organization).append(" - ").append(this.title);
 	    
 	    return result.toString();
 	}
}
